package com.kpj.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class KeyboardTestActivity extends Activity {
	private final static float	RATIO 			= 0.625f; //16:10
	private final static int	DURATION 		= 500;
	private LinearLayout 		keyboard;
	private Button 				clickButton;
	private boolean				isInAnimating;
	private boolean 			isPopuped;
	private boolean				isAnimationInitialized;
	private Animation			upAnimation;
	private Animation			downAnimation;
	private int					displayWidth;
	private int					displayHeight;
	private int					keyboardWidth;
	private int					keyboardHeight;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keyboard);
        
        init();
    }
    
    private void init() {
    	keyboard = (LinearLayout) findViewById(R.id.ll_main_keyboard);
    	initSizeByRatio();
    	initAnimation();
    	clickButton = (Button) findViewById(R.id.btn_main_click);
    	clickButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (isInAnimating)
					return;
				
				if (!isPopuped) {
					upAnimationStart();
					isPopuped = true;
				} else {
					downAnimationStart();
					isPopuped = false;
				}
			}
		});
    }
    
    private void initSizeByRatio() {
    	WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    	Display defaultDisplay = windowManager.getDefaultDisplay();
    	displayWidth = defaultDisplay.getWidth();
    	displayHeight = defaultDisplay.getHeight();
    	keyboardWidth = displayWidth;
    	keyboardHeight = (int) (keyboardWidth * RATIO);
    	LayoutParams keyboardLayoutParams = new LayoutParams(keyboardWidth, keyboardHeight);
    	keyboardLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    	keyboard.setLayoutParams(keyboardLayoutParams);
    }
    
    private void initAnimation() {
    	upAnimation = new TranslateAnimation(0.0f, 0.0f, displayHeight - keyboardHeight, 0.0f);
    	upAnimation.setDuration(DURATION);
    	upAnimation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_decelerate_interpolator));
    	upAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				keyboard.setVisibility(View.VISIBLE);
				isInAnimating = true;
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				isInAnimating = false;
			}
		});
    	
    	downAnimation = new TranslateAnimation(0.0f, 0.0f, 0.0f, displayHeight - keyboardHeight);
    	downAnimation.setDuration(DURATION);
    	downAnimation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_decelerate_interpolator));
    	downAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				isInAnimating = true;
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				keyboard.setVisibility(View.GONE);
				isInAnimating = false;
			}
		});
    }
    
    private void upAnimationStart() {
    	keyboard.startAnimation(upAnimation);
    }
    
    private void downAnimationStart() {
    	keyboard.startAnimation(downAnimation);
    }

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if (!isAnimationInitialized) {
			initAnimation();
			isAnimationInitialized = true;
		}
		
		super.onWindowFocusChanged(hasFocus);
	}
}